package com.dd.dus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.dc.entity.users.DRole;
import com.dd.dc.entity.users.DRolePermissionRelation;
import com.dd.dc.entity.users.DUserRoleRelation;
import com.dd.dus.service.impl.DRolePermissionRelationServiceImpl;
import com.dd.dus.service.impl.DRoleServiceImpl;
import com.dd.dus.service.impl.DUserRoleRelationServiceImpl;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author JueDream
 * @since 2021-03-05
 */
@Slf4j
@RestController
@RequestMapping("/user/d-role")
public class DRoleController {

    @Autowired
    DRoleServiceImpl roleService;

    @Autowired
    DUserRoleRelationServiceImpl userRoleRelationService;

    @Autowired
    DRolePermissionRelationServiceImpl rolePermissionRelationService;

    @ApiOperation(value = "角色分页列表查询接口")
    @PostMapping("/queryRoleList")
    public Page<DRole> queryRoleList(@RequestParam int page, @RequestParam int limit, @RequestParam String roleName){
        log.info("UserServcie queryRoleList start ... {}");
        return roleService.queryRoleList(page, limit, roleName);
    }

    @ApiOperation(value = "角色列表查询接口")
    @PostMapping("/findRoleByUserId")
    public List<DRole> findRoleByUserId(@RequestParam Integer userId){
        log.info("UserServcie findRoleByUserId start ... {}", userId);
        return roleService.findRoleByUserId(userId);
    }

    @ApiOperation(value = "用户角色关系新增接口")
    @PostMapping("/saveBatchUR")
    public boolean saveBatchUR(@RequestBody List<DUserRoleRelation> roleRelation){
        log.info("UserServcie saveBatchUR start ... {}", roleRelation);
        return userRoleRelationService.saveBatch(roleRelation);
    }

    @ApiOperation(value = "用户角色关系删除接口")
    @DeleteMapping("/deleteUR")
    public boolean deleteUR(@RequestParam Integer userId){
        log.info("UserServcie deleteUR start ... {}", userId);
        QueryWrapper<DUserRoleRelation> wrapper = new QueryWrapper();
        wrapper.eq("USER_ID", userId);
        return userRoleRelationService.remove(wrapper);
    }

    @ApiOperation(value = "角色删除接口")
    @DeleteMapping("/delete")
    public boolean delete(@RequestParam Integer roleId){
        return roleService.removeById(roleId);
    }

    @ApiOperation(value = "用户角色关系查詢接口")
    @PostMapping("/queryRelationByRoleId")
    public boolean queryRelationByRoleId(@RequestParam Integer roleId){
        boolean flag = true;
        log.info("UserServcie queryRelationByRoleId start ... {}", roleId);
        List<DUserRoleRelation> roleRelations = userRoleRelationService.queryRelationByRoleId(roleId);
        if (roleRelations.size() > 0){
            flag = false;
        }
        return flag;
    }

    @ApiOperation(value = "角色权限关系新增接口")
    @PostMapping("/saveBatchRP")
    public boolean saveBatchRP(@RequestBody List<DRolePermissionRelation> rolePermissionRelations){
        log.info("UserServcie saveBatchRP start ... {}", rolePermissionRelations);
        return rolePermissionRelationService.saveBatch(rolePermissionRelations);
    }

    @ApiOperation(value = "角色权限关系删除接口")
    @DeleteMapping("/deleteRP")
    public boolean deleteRP(@RequestParam Integer roleId){
        log.info("UserServcie deleteRP start ... {}", roleId);
        QueryWrapper<DRolePermissionRelation> wrapper = new QueryWrapper();
        wrapper.eq("ROLE_ID", roleId);
        return rolePermissionRelationService.remove(wrapper);
    }

    @ApiOperation(value = "角色新增接口")
    @PostMapping("/insert")
    public Integer insert(@RequestBody DRole role){
        log.info("UserServcie insert start ... {}", role);
        return roleService.insert(role);
    }

    @ApiOperation(value = "角色更新接口")
    @PostMapping("/updateById")
    public boolean updateById(@RequestBody DRole role){
        log.info("UserServcie updateById start ... {}", role);
        return roleService.updateById(role);
    }


}
